<?php

namespace app\controller;

use app\BaseController;
use app\model\PartnerUser;
use app\service\NetworkBussinessService;
use app\service\NetworksService;
use think\exception\ValidateException;
use think\facade\Request;

/**
 * 网点
 */
class Networks extends BaseController
{
    /**
     * 网点列表
     * @return void
     */
    public function lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['business_id'] = Request::param('business_id/d', 0);
        $param['network_ids'] = Request::param('network_ids/s', '');
        $param['company_name'] = Request::param('company_name/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['service_status'] = Request::param('service_status/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $param['is_export'] = Request::param('is_export/d', 0);
        $param['uid'] = $uid;
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $logParam['ip'] = Request::ip();
        $data = NetworksService::lists($param);
        if ($data) {
            returnSuccess(200, '获取成功', $data);
        } else {
            returnFail(21101, '获取失败');
        }
    }

    /**
     * @desc 网点 业务范围
     * @return void
     */
    public function info()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['uid'] = $uid;
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $param['is_export'] = Request::param('is_export/d', 0);
        $info = NetworkBussinessService::listsInfo($param);
        returnSuccess(200, '获取成功', $info);
    }

    //新增网点
    public function add()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['charge_person'] = Request::param('charge_person/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['province_id'] = Request::param('province_id/d', 0);
        $param['city_id'] = Request::param('city_id/d', 0);
        $param['district_id'] = Request::param('district_id/d', 0);
        $param['longitude'] = Request::param('longitude/s', '');
        $param['dimension'] = Request::param('dimension/s', '');
        $param['address'] = Request::param('address/s', '');
        $param['business_id'] = Request::param('business_id/s', '');
        $param['status'] = Request::param('status/d', 1);
        $param['business_type'] = Request::param('business_type/d', 1);

        $param['company_id'] = Request::param('company_id/d', 0);
        
        $param['receiver_name'] = Request::param('receiver_name/s', '');
        $param['receiver_mobile'] = Request::param('receiver_mobile/d', '');
        $param['receiver_province_id'] = Request::param('receiver_province_id/d', 0);
        $param['receiver_city_id'] = Request::param('receiver_city_id/d', 0);
        $param['receiver_district_id'] = Request::param('receiver_district_id/d', 0);
        $param['receiver_address'] = Request::param('receiver_address/s', '');
        $param['receiver_longitude'] = Request::param('receiver_longitude', 0.00);
        $param['receiver_dimension'] = Request::param('receiver_dimension', 0.00);

        $param['bd_gps_longitude'] = Request::param('longitude/s', '');
        $param['bd_gps_dimension'] =  Request::param('dimension/s', '');

        $param['money'] = Request::param('money/f', 0.00);
        $param['driver_lv'] = Request::param('driver_lv/f', 0.00);
        $param['company_lv'] = Request::param('company_lv/f', 0.00);
        $param['group_lv'] = Request::param('group_lv/f', 0.00);
        $param['uid'] =  PartnerUser::where('id',$uid)->value('source');
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\Networks::class)->check([
                'name' => $param['name'],
                'charge_person' => $param['charge_person'],
                'mobile' => $param['mobile'],
                'province_id' => $param['province_id'],
                'city_id' => $param['city_id'],
                'district_id' => $param['district_id'],
                'longitude' => $param['longitude'],
                'dimension' => $param['dimension'],
                'address' => $param['address'],
                // 'business_id' => $param['business_id']
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        // if(LonGrep($param['longitude'])==false){
        //     returnFail(21201,'经度格式不正确');
        // }
        // if(LatGrep($param['dimension'])==false){
        //     returnFail(21202,'纬度度格式不正确');
        // }
        $data = NetworksService::add($param, $logParam);
        if ($data == 10000) {
            returnSuccess(200, '添加成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //修改仓库
    public function modify()
    {
        $uid = Request::param('uid/d', 0);
        $id = Request::param('id/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['charge_person'] = Request::param('charge_person/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['province_id'] = Request::param('province_id/d', 0);
        $param['city_id'] = Request::param('city_id/d', 0);
        $param['district_id'] = Request::param('district_id/d', 0);
        $param['longitude'] = Request::param('longitude/s', '');
        $param['dimension'] = Request::param('dimension/s', '');
        $param['address'] = Request::param('address/s', '');
         $param['business_id'] = Request::param('business_id/s', '');
        $param['status'] = Request::param('status/d', 1);
        $param['business_type'] = Request::param('business_type/d', 1);

        $param['company_id'] = Request::param('company_id/d', 0);
        
        $param['receiver_name'] = Request::param('receiver_name/s', '');
        $param['receiver_mobile'] = Request::param('receiver_mobile/d', '');
        $param['receiver_province_id'] = Request::param('receiver_province_id/d', 0);
        $param['receiver_city_id'] = Request::param('receiver_city_id/d', 0);
        $param['receiver_district_id'] = Request::param('receiver_district_id/d', 0);
        $param['receiver_address'] = Request::param('receiver_address/s', '');

        $param['receiver_longitude'] = Request::param('receiver_longitude', 0.00);
        $param['receiver_dimension'] = Request::param('receiver_dimension', 0.00);

        $param['bd_gps_longitude'] = Request::param('longitude/s', '');
        $param['bd_gps_dimension'] =  Request::param('dimension/s', '');

        $param['money'] = Request::param('money/f', 0.00);
        $param['driver_lv'] = Request::param('driver_lv/f', 0.00);
        $param['company_lv'] = Request::param('company_lv/f', 0.00);
        $param['group_lv'] = Request::param('group_lv/f', 0.00);
        
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\Networks::class)->check([
                'name' => $param['name'],
                'charge_person' => $param['charge_person'],
                'mobile' => $param['mobile'],
                'province_id' => $param['province_id'],
                'city_id' => $param['city_id'],
                'district_id' => $param['district_id'],
                'longitude' => $param['longitude'],
                'dimension' => $param['dimension'],
                'address' => $param['address'],
                // 'business_id' => $param['business_id']
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        // if(LonGrep($param['longitude'])==false){
        //     returnFail(21201,'经度格式不正确');
        // }
        // if(LatGrep($param['dimension'])==false){
        //     returnFail(21202,'纬度度格式不正确');
        // }
        $data = NetworksService::edit($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '修改成功');
        } else {
            returnFail(21101, '修改失败');
        }
    }

    //删除仓库
    public function del()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $id = Request::param('id/d', 0);
        $logParam['ip'] = Request::ip();
        $data = NetworksService::del($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '删除成功');
        } else {
            returnFail(21101, '删除失败');
        }
    }
}
